//
//  TabBarViewController.m
//  LoveLove
//
//  Created by wkf on 2019/3/27.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "TabBarViewController.h"
#import "LLFirstViewViewController.h"
#import "LLGradeVC.h"
#import "LLPublishViewController.h"
#import "LLBasePublishViewController.h"
#import "LLMallViewController.h"
#import "LLMyViewController.h"
#import "MCTabBar.h"
#import "QWebVC.h"
#import "LLCentVC.h"

@interface TabBarViewController ()<UITabBarControllerDelegate>
@property (nonatomic, assign) NSUInteger selectItem;//选中的item
@property (nonatomic, strong) MCTabBar *mcTabbar;
@property (assign, nonatomic) BOOL showVertifyView;

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_mcTabbar removeFromSuperview];
    [self.tabBar removeFromSuperview];
    _mcTabbar = [[MCTabBar alloc] init];
    [_mcTabbar.centerBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    //选中时的颜色
    _mcTabbar.tintColor = [UIColor colorWithRed:27.0/255.0 green:118.0/255.0 blue:208/255.0 alpha:1];
    //透明设置为NO，显示白色，view的高度到tabbar顶部截止，YES的话到底部
    _mcTabbar.translucent = NO;
    //利用KVC 将自己的tabbar赋给系统tabBar
    [self setValue:_mcTabbar forKeyPath:@"tabBar"];
    
    self.selectItem = 0; //默认选中第一个
    self.delegate = self;
    
    // 首页
   LLNavViewController *nav1 = [self setupChildVc:LLFirstViewViewController.new title:@"首页" image:@"发现-1" selectedImage:@"发现"];
    // 课程
    LLNavViewController *nav2 = [self setupChildVc:LLGradeVC.new title:@"年级" image:@"graduation_cap_128px_1101575_easyicon.net" selectedImage:@"graduation_cap_128px_1101575_easyicon.net-1"];
    // 发布
    LLNavViewController *nav3 = [self setupChildVc:LLBasePublishViewController.new title:@"发布" image:@"" selectedImage:@""];
    
    LLCentVC *vc = [LLCentVC new];
    vc.urlString = [NSString stringWithFormat:@"http://www.haoaih.com/h5/users/get_point?u_id=%@&num=%@",[LLUserInfo sharedUser].user_id,[LLUserInfo sharedUser].user_random];
    vc.showNavBar = NO;

    // 商城
   LLNavViewController *nav4 =  [self setupChildVc:vc title:@"积分" image:@"矩形 25 拷贝" selectedImage:@"矩形 26"];

    // 我的
    LLNavViewController *nav5 = [self setupChildVc:LLMyViewController.new title:@"我的" image:@"Person_115.52436647173px_1142788_easyicon.net" selectedImage:@"Person_115.52436647173px_1142788_easyicon.net-1"];
    
    //底部tab文字的颜色和字体大小
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:LLColor(0, 0, 0),NSFontAttributeName:LLRegularFont(12)} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:LLRegularFont(12)} forState:UIControlStateSelected];
    self.tabBar.barTintColor = LLColorRGBAlpha(255, 255, 255, 0.23);
    self.tabBar.translucent = NO;
    self.viewControllers = @[nav1,nav2,nav3,nav4,nav5];
}

- (LLNavViewController *)setupChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    LLNavViewController *nav = [[LLNavViewController alloc] initWithRootViewController:childVc];
    nav.tabBarItem.title = title;
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置普通状态下的文字
    NSMutableDictionary *normalAttr = [NSMutableDictionary dictionary];
    normalAttr[NSForegroundColorAttributeName] = LLColor(0, 0, 0);
    normalAttr[NSFontAttributeName] = LLRegularFont(9);
    [childVc.tabBarItem setTitleTextAttributes:normalAttr forState:UIControlStateNormal];
    //设置选中状态下的文字
    NSMutableDictionary *selectedAttr = [NSMutableDictionary dictionary];
    selectedAttr[NSForegroundColorAttributeName] = LLColor(0, 0, 0);
    selectedAttr[NSFontAttributeName] = LLRegularFont(9);
    [childVc.tabBarItem setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
//    [self addChildViewController:nav];
    return nav;
}

#pragma mark - 点击中间凸起的按钮
- (void)buttonAction:(UIButton *)button{
    LLPublishViewController *vc = [LLPublishViewController new];
    vc.selectedindex = self.selectedIndex;
    LLNavViewController *navi = [[LLNavViewController alloc] initWithRootViewController:vc];
    [[UIViewController currentViewController] presentViewController:navi animated:YES completion:nil];
    self.selectItem = 2;
}

#pragma mark - tabbar选择时的代理
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
{
    if (tabBarController.selectedIndex != 2){//选中中间的按钮
        [_mcTabbar.centerBtn.layer removeAllAnimations];
        self.selectedIndex  = tabBarController.selectedIndex;
    }
    self.selectItem = tabBarController.selectedIndex;
}

- (void)dealloc {
    [LLNotificationCenter removeObserver:self];
}


@end
